home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / CHIPCD_02_2002.iso / Internet / Macromedia ColdFusion Server 5 / coldfusion-50-win-us.exe / data1.cab / Examples / CFDOCS / snippets / structkeyarray.cfm < prev    next >
Encoding:
Text File  |  2001-06-13  |  2.8 KB  |  84 lines

  1. <!--- This example shows how to use the StructKeyArray
  2.       function to copy the keys from a specified structure to an array.
  3.       It also uses the StructNew function to create the structure
  4.       and fills its fields with the information the user types 
  5.       into the corresponding form fields. --->
  6. <HTML>
  7. <HEAD>
  8. <title>StructKeyArray Function</title>
  9. </HEAD>
  10.  
  11. <BASEFONT FACE="Arial, Helvetica" SIZE=2>
  12.  
  13. <BODY  bgcolor="#FFFFD5">
  14.  
  15. <H3>StructKeyArray Example</H3>
  16. <H3>Extracting the Keys from the Employee Structure</H3>      
  17.  
  18. <!--- This section of code creates the new structure and checks to 
  19.       see if the submit button has been pressed.  If it has been
  20.       pressed, the code defines fields in the employee structure
  21.       with what the user has entered from the form. --->
  22. <CFSET employee=StructNew()>  
  23. <CFIF Isdefined("Form.Submit")>
  24.     <CFIF Form.Submit is "OK">
  25.         <CFSET employee.firstname = FORM.firstname>
  26.         <CFSET employee.lastname = FORM.lastname>
  27.         <CFSET employee.email = FORM.email>
  28.         <CFSET employee.phone = FORM.phone>
  29.         <CFSET employee.company = FORM.company> 
  30.     <CFELSEIF Form.Submit is "Clear">
  31.         <CFSET rc=StructClear(employee)>
  32.     </CFIF>
  33. </CFIF>      
  34.       
  35. <P>
  36. This example uses the StructNew function to create a structure 
  37. that supplies employee information.  The data structure is called 
  38. "employee" and its fields are filled with the contents of the 
  39. following form. After you have entered employee information into the 
  40. structure, the example uses the <b>StructKeyArray</b> function to copy 
  41. all of the keys from the structure into an array.
  42. </P>
  43.  
  44. <HR size="2" color="#0000A0">
  45.  
  46. <FORM action="structkeyarray.cfm" method="post">
  47. <table cellspacing="2" cellpadding="2" border="0">
  48.     <tr>
  49.     <td>First Name:</td>
  50.     <td><INPUT name="firstname" type="text" value="" hspace="30" maxlength="30"></td>
  51.     </tr>
  52.     <tr>
  53.     <td>Last Name:</td>
  54.     <td><INPUT name="lastname" type="text" value="" hspace="30" maxlength="30"></td>
  55.     </tr>
  56.     <tr>
  57.     <td>EMail</td>
  58.     <td><INPUT name="email" type="text" value="" hspace="30" maxlength="30"></td>
  59.     </tr>
  60.     <tr>
  61.     <td>Phone:</td>
  62.     <td><INPUT name="phone" type="text" value="" hspace="20" maxlength="20"></td>
  63.     </tr>
  64.     <tr>
  65.     <td>Company:</td>
  66.     <td><INPUT name="company" type="text" value="" hspace="30" maxlength="30"></td>
  67.     </tr>
  68.     <tr>
  69.     <td><input type="submit" name="submit" value="OK"></td>
  70.     <td><b>After you submit the form, scroll down to see the array.</b></td>
  71.     </tr>
  72. </table>
  73. </FORM>
  74.  
  75. <CFIF NOT StructISEmpty(employee)> 
  76.     <HR size="2" color="#0000A0">
  77.     <CFSET keysToStruct = StructKeyArray(employee)>
  78.     <CFLOOP index="i" from="1" to="#ArrayLen(keysToStruct)#">
  79.         <p><CFOUTPUT>Key#i# is #keysToStruct[i]#</CFOUTPUT></p>
  80.         <p><CFOUTPUT>Value#i# is #employee[keysToStruct[i]]#</CFOUTPUT></p>
  81.     </CFLOOP>
  82. </CFIF>
  83. </BODY>
  84. </HTML>